package com.hfzy.ihk.web.userCenter.controller;

import com.hfzy.ihk.facade.userCenter.entity.OAuthClient;
import com.hfzy.ihk.facade.userCenter.entity.UserInfo;
import com.hfzy.ihk.web.oauth.client.table.PageTableHandler;
import com.hfzy.ihk.web.oauth.client.table.PageTableRequest;
import com.hfzy.ihk.web.oauth.client.table.PageTableResponse;
import com.hfzy.ihk.web.userCenter.mapper.OAuthMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by YANFA on 2019/3/4.
 */
@RestController
@RequestMapping("oauthClient")
@Slf4j
public class OAuthController {

    @Autowired
    OAuthMapper oAuthMapper;

    /**
     * 授权中心客户端管理界面
     * @param view
     * @return
     */
    @RequestMapping("oauthClientPage")
    public ModelAndView oauthClientPage(ModelAndView view) {
        view.setViewName("page/oauth/oauthClientList");
        return view;
    }


    /**
     * 查询方法
     * @param request
     * @return
     */

    @RequestMapping("oauthClientList")
    public PageTableResponse listUsers(PageTableRequest request) {
        request.setOffset((request.getOffset()-1)*request.getLimit());
        return new PageTableHandler(new PageTableHandler.CountHandler() {

            @Override
            public int count(PageTableRequest request) {

                return oAuthMapper.count(request.getParams());
            }
        }, new PageTableHandler.ListHandler() {

            @Override
            public List<OAuthClient> list(PageTableRequest request) {
                List<OAuthClient> list = oAuthMapper.list(request.getParams(), request.getOffset(), request.getLimit());
                return list;
            }
        }).handle(request);
    }


    @RequestMapping("updateWebServerRedirectUri")
    public Map<String,Object> updateWebServerRedirectUri(String webServerRedirectUri,String clientId){
        Map<String,Object> map = new HashMap<>();
        try{
            oAuthMapper.updateWebServerRedirectUri(webServerRedirectUri,clientId);
            map.put("code",1);
            map.put("msg","更新成功");

        }catch (Exception e){
            log.error("更新授权白名单失败",e);
            map.put("code",0);
            map.put("msg","更新失败");
        }

        return map;
    }

}
